#include "mainscene.h"
#include "ui_mainscene.h"
#include<QTimer>
#include <QMessageBox>
#include<QObject>
/************/
int ball_skin = 1;
/************/
MainScene::MainScene(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MainScene)
{

    music_on = true;
    py=-900;
    QTimer* timer = new QTimer(this);

    QMediaPlaylist *musicList=new QMediaPlaylist(this);
    musicList->addMedia(QUrl("qrc:/sucai/mmain.wav"));
    musicList->setPlaybackMode(QMediaPlaylist::CurrentItemInLoop);
    QMediaPlayer *soundPlayer=new QMediaPlayer(this);
    soundPlayer->setPlaylist((musicList));
    soundPlayer->play();


    timer->start(10);

    connect(timer,&QTimer::timeout,[=]()
    {
        if(music_on==true)
        {
            soundPlayer->play();
        }
        else if(music_on==false)
        {
            soundPlayer->pause();
        }


        py+=1;
        if(py<=0)
         update();
        else if(py>0){
            py = -895;
            update();
        }

    });

    ui->setupUi(this);
    inite_scene();
    ui->startButton->setStyleSheet("color:white");
    ui->setButton->setStyleSheet("color:white");
    ui->quitButton->setStyleSheet("color:white");
    ui->change->setStyleSheet("color:white");
}

MainScene::~MainScene()
{
    delete ui;

}


//初始化初始界面
void MainScene::inite_scene()
{
   setFixedSize(SCENE_WIDTH,SCENE_HEIGHT);
   setWindowTitle(GAME_NAME);
   setWindowIcon(QIcon(":/sucai/ball.PNG"));
}
//退出确认
void MainScene::closeEvent(QCloseEvent *event)
{
   int ret = QMessageBox::question(this,"请问","确认退出游戏？");
   if(ret == QMessageBox::Yes)
        event->accept();
   else
       event->ignore();
}

//画画
void MainScene::paintEvent(QPaintEvent *)
{
    QPainter pt(this);
    pt.drawPixmap(0,py,800,1800,QPixmap(":/sucai/background -new.png"));

    QPainter npt(this);
    npt.drawPixmap(100,100,650,400,QPixmap(":/sucai/headline.PNG"));
}

//点击开始按钮
void MainScene::on_startButton_clicked()
{
    this->hide();
    emit showsetscene();
}

void MainScene::reshow()
{
    this->show();
}

//点击退出按钮
void MainScene::on_quitButton_clicked()
{
    this->close();
}

void MainScene::on_setButton_clicked()
{
    emit showhelper();
}

void MainScene::on_music_clicked()
{
    if(music_on){
        QIcon musicoff(":/sucai/music_off.png");
        ui->music->setIcon(musicoff);
        music_on = false;
    }
    else
    {
        QIcon musicon(":/sucai/music_on.png");
        ui->music->setIcon(musicon);
        music_on = true;
    }

}

void MainScene::on_change_clicked()
{
    emit showballskin();
}
